def max_m_partitions(nums):
    sum_total = sum(nums)
    
    # 生成所有因数并按从大到小排序
    def get_factors(total):
        factors = set()
        for i in range(1, int(total**0.5) + 1):
            if total % i == 0:
                factors.add(i)
                factors.add(total // i)
        return sorted(factors, reverse=True)
    
    factors = get_factors(sum_total)
    
    # 尝试每个因数作为 m
    for m in factors:
        target = sum_total // m
        current_sums = [0] * m  # 记录每个子集的当前和
        
        def can_partition(index):
            if index == len(nums):
                return all(s == target for s in current_sums)
            
            for i in range(m):
                # 剪枝1：若当前子集非空且与前一子集相同，跳过
                if i > 0 and current_sums[i] == current_sums[i - 1]:
                    continue
                # 剪枝2：若当前子集加上当前元素超过目标值，跳过
                if current_sums[i] + nums[index] > target:
                    continue
                
                # 尝试将当前元素放入第 i 个子集
                current_sums[i] += nums[index]
                if can_partition(index + 1):
                    return True
                # 回溯
                current_sums[i] -= nums[index]
                
                # 剪枝3：若此子集为空且无法放置当前元素，则后续也无解
                if current_sums[i] == 0:
                    break
            
            return False
        
        if can_partition(0):
            return m
    
    return 1  # 如果没有找到更大的 m，至少可以分成 1 份



# 测试用例
if __name__ == "__main__":    
    # 测试用例
    test_cases = [
        [6, 4, 3, 3, 2],
        [10, 8, 6, 4, 2],
        [7, 7, 7],
        [5, 4, 3, 2, 1],
        [5, 4, 3, 2],
    ]
    
    for i, test in enumerate(test_cases):
        print(f"测试用例 {i+1} {test}: 最大的m值为 {max_m_partitions(test)}")